package com.yanqu.road.server.cmd.building;

import com.yanqu.road.entity.config.building.CollectionUpgrade;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserCollection;
import com.yanqu.road.pb.building.BuildingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CollectionModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.config.CollectionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_COLLECTION_UPGRADE, desc = "藏品升级")
public class CollectionUpgradeCmd extends AbstractCommand {

	private int collectionId;

	private boolean isSuccess;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BuildingProto.CollectionUpgradeReqMsg reqMsg = BuildingProto.CollectionUpgradeReqMsg.parseFrom(packet.getBytes());
		collectionId = reqMsg.getCollectionId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		CollectionModule collectionModule = player.getModule(CollectionModule.class);
		UserCollection userCollection = collectionModule.getUserCollection(collectionId);
		if(null == userCollection){
			ret = GameErrorCode.E_COLLECTION_NO_FOUND;
			return;
		}
		CollectionUpgrade nextCollectionUpgrade = CollectionMgr.getCollectionUpgrade(userCollection.getLevel() + 1);
		if(null == nextCollectionUpgrade){
			ret = GameErrorCode.E_COLLECTION_MAX_LEVEL;
			return;
		}
		CollectionUpgrade collectionUpgrade = CollectionMgr.getCollectionUpgrade(userCollection.getLevel());
		if(player.getModule(CurrencyModule.class).removeCurrency(collectionUpgrade.getConsume(), eLogMoneyType.Building, eLogMoneyType.CollectionUpgrade)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		isSuccess = collectionModule.collectionUpgrade(collectionId);
	}

	@Override
	public void sendData(GamePlayer player) {
		BuildingProto.CollectionUpgradeRespMsg.Builder respMsg = BuildingProto.CollectionUpgradeRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setIsSuccess(isSuccess);
		}
		player.sendPacket(Protocol.U_COLLECTION_UPGRADE, respMsg);
	}

}
